
let env:string = wx.getAccountInfoSync().miniProgram.envVersion
let localEnv:string = wx.getStorageSync('localEnv') || ''
// env = 'release'
// console.log('env:',env,localEnv)
if (env !== localEnv) {
  wx.clearStorageSync()
  wx.setStorageSync('localEnv', env)
}

const globalConfig:any = {
  develop:{
    url:'https://d-record-api.wanhuhealth.com'
  },
  trial:{
    url:'https://d-record-api.wanhuhealth.com'
  },
  release:{
    url:'https://record-api.wanhuhealth.com'
  }
}


App<IAppOption>({
  globalData: {
    url:globalConfig[env].url || 'https://d-record-api.wanhuhealth.com',
    defaultShareData:{
      title:'我记了一篇日记，快来看看吧',
      path:'/pages/index/index?shareChannel=1',
      imageUrl:'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/share.png?v=2'
    },
    isDev: env !== 'release',
    navBarHeight:'160rpx',
    _audioContext:null,
    hasRecorded:false  // 当前页面是否录音过
  },
  onShow(options:any) {
    // console.log('onLaunch options:',options)
    let launchOptions = wx.getLaunchOptionsSync()
    let query = launchOptions.query
    let that = this
    // 微信登录
    wx.setKeepScreenOn({
      keepScreenOn:true
    })
    // 删除旧的token
    // wx.removeStorageSync('token')
    wx.login({
      success:(res)=>{
        // console.log('login res:',res)
        if(res.code){
          // 发起网络请求，获取openId等信息
          let loginUrl = that.globalData.url + '/wechatapp/unionid'
          
          let reqData:any = {
            code:res.code
          }
          if(Object.keys(query).length > 0){
            reqData.inviteUserId = query.inviteUserId
            reqData.shareChannel = query.shareChannel
          }

          wx.request({
            url: loginUrl,
            method: 'POST',
            data:reqData,
            success: function (res:any) {
              // console.log('res:',res)
              if(res.data.code === 0){
                wx.setStorageSync('token',res.data.data.token)
                wx.setStorageSync('userId',res.data.data.userId)
              }
            },
            fail(err){
              console.log('error:',err)
            }
          })
        }
        // console.log('login res:',res)
      }
    })
    
    
    // 设置音频选项
    wx.setInnerAudioOption({
      mixWithOther: false,
      obeyMuteSwitch: false,
      success: function (e) {
        console.log('setInnerAudioOption play success：',e)
      },
      fail: function (e) {
        // console.log('play fail:',e)
      }
    }),
    // 获取小程序高度
    this.getMiniProgramMenuBarHeight()
    // 加载字体，使用了css加载，暂时不用js加载
    let fontFamily = query.fontFamily || wx.getStorageSync('fontFamily')
    wx.setStorageSync('fontFamily',fontFamily)
    this.loadFont(fontFamily)
    // 检查在线升级
    this.checkUpdate()
    // 初始化音频播放器
    // this.initAudioContext()

    // 之前onshow的方法
    let startData = wx.getLaunchOptionsSync()
    let inviteUserId = startData?.query.inviteUserId
    if(inviteUserId){
      wx.setStorageSync('inviteUserId',inviteUserId)
    }
    wx.setStorageSync('hasShowModalError',false)
  },
  // onShow(){
  //   let startData = wx.getLaunchOptionsSync()
  //   let inviteUserId = startData?.query.inviteUserId
  //   if(inviteUserId){
  //     wx.setStorageSync('inviteUserId',inviteUserId)
  //   }
  //   wx.setStorageSync('hasShowModalError',false)
  // },
 
  getDefaultShareData(){
    let userInfo = wx.getStorageSync('userInfo')
    let userId = (userInfo && userInfo.id) || ''
    let path = this.globalData.defaultShareData.path
    if(path.includes('?')){
      path = path+'&inviteUserId=' + userId+'&shareChannel=1'
    }else {
      path = path+'?inviteUserId=' + userId+'&shareChannel=1'
    }
    return {
      title:this.globalData.defaultShareData.title,
      path: path,
      imageUrl:this.globalData.defaultShareData.imageUrl,
    }
  },
  initAudioContext(){
    this.globalData._audioContext = wx.createInnerAudioContext({
      useWebAudioImplement: true 
    }) as any
    
  },
  checkUpdate(){
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          wx.clearStorageSync()
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content:
          '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  getMiniProgramMenuBarHeight(){
    let systemInfo = wx.getWindowInfo()
    let menuButtonRect = wx.getMenuButtonBoundingClientRect()
    let barHeight = (menuButtonRect.top - systemInfo.statusBarHeight) * 2 + menuButtonRect.height + systemInfo.statusBarHeight
    // console.log('barHeight:',barHeight)
    this.globalData.navBarHeight = barHeight + 'px'
  },
  loadFont(fontFamily = '' ){
    // let fontMap: {[key:string]:string}  = {
    //   '瑞美加张清平硬笔行书':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E7%91%9E%E7%BE%8E%E5%8A%A0%E5%BC%A0%E6%B8%85%E5%B9%B3%E7%A1%AC%E7%AC%94%E8%A1%8C%E4%B9%A6.ttf',
    //   '千图笔锋手写体':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E5%8D%83%E5%9B%BE%E7%AC%94%E9%94%8B%E6%89%8B%E5%86%99%E4%BD%93.ttf',
    //   '千图纤墨体':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E5%8D%83%E5%9B%BE%E7%BA%A4%E5%A2%A8%E4%BD%93.ttf',
    //   '黄凯桦律师手写体':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E9%BB%84%E5%87%AF%E6%A1%A6%E5%BE%8B%E5%B8%88%E6%89%8B%E5%86%99%E4%BD%93.ttf',
    //   '寒蝉活楷体':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/ChillHuoKai_Regular.otf',
    //   '演示夏行楷':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E6%BC%94%E7%A4%BA%E5%A4%8F%E8%A1%8C%E6%A5%B7.ttf',
    //   '马善政毛笔楷书':'https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E9%A9%AC%E5%96%84%E6%94%BF%E6%AF%9B%E7%AC%94%E6%A5%B7%E4%B9%A6.ttf'
    // }
    // let fontUrl = fontMap[fontFamily] 
    // console.log('loadFont fontFamily:',fontFamily)
    // console.log('loadFont fontUrl:',fontUrl)
    wx.loadFontFace({
      family: 'rijiti',
      global: true,
      source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E7%91%9E%E7%BE%8E%E5%8A%A0%E5%BC%A0%E6%B8%85%E5%B9%B3%E7%A1%AC%E7%AC%94%E8%A1%8C%E4%B9%A6.ttf")',
      success: (res)=>{
        // console.log('zhushiti res:',res)
      },
      fail:(error)=>{
        console.error('zhushiti error:',error)
      }
    })
    wx.loadFontFace({
      family: 'shigeti',
      global: true,
      source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/font/%E5%8D%83%E5%9B%BE%E7%AC%94%E9%94%8B%E6%89%8B%E5%86%99%E4%BD%93.ttf")',
      success: (res)=>{
        // console.log('zhushiti res:',res)
      },
      fail:(error)=>{
        console.error('zhushiti error:',error)
      }
    })
    wx.loadFontFace({
      family: 'shuxinti',
      global: true,
      // source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/TaiWanQuanZiKuZhengKaiTi-2.ttf")',
      source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/shuxin.ttf")',
      success: (res)=>{
        // console.log('TaiWanQuanZiKuZhengKaiTi res:',res)
      },
      fail:(error)=>{
        console.error('TaiWanQuanZiKuZhengKaiTi error:',error)
      }
    })
    wx.loadFontFace({
      family: 'chunjieti',
      global: true,
      // source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/TaiWanQuanZiKuZhengKaiTi-2.ttf")',
      source: 'url("https://wanhuhealth.oss-cn-beijing.aliyuncs.com/static/note-miniProgram/chunjiezhufu.ttf")',
      success: (res)=>{
        // console.log('TaiWanQuanZiKuZhengKaiTi res:',res)
      },
      fail:(error)=>{
        console.error('chunjieti error:',error)
      }
    })
  }

})